﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using EzPos.Model.Common;
using EzPos.Model.Product;
using EzPos.Properties;

namespace EzPos.GUIs.Forms
{
    public class PrintProduct
    {
        private static readonly PrintDocument PrintDocument = new PrintDocument();
        private static int _counter;
        private static BindingList<Product> _productListt = new BindingList<Product>();
        private static StringFormat _strFormat;

        public static void InializePrinting(BindingList<Product> productList)
        {
            _productListt = productList;
            var printPreviewDialog = new PrintPreviewDialog
                                         {
                                             WindowState = FormWindowState.Maximized,
                                             FormBorderStyle = FormBorderStyle.None,
                                             UseAntiAlias = true,
                                             Document = PrintDocument
                                         };
            PrintDocument.PrinterSettings.PrinterName = AppContext.Counter.ReportPrinter;

            PrintDocument.BeginPrint += PrintDocBeginPrint;
            PrintDocument.PrintPage += PrintDocumentPrintPage;

            printPreviewDialog.ShowDialog();

            PrintDocument.BeginPrint -= PrintDocBeginPrint;
            PrintDocument.PrintPage -= PrintDocumentPrintPage;
        }

        private static void PrintDocumentPrintPage(object sender, PrintPageEventArgs e)
        {
            var posY = 30;
            var rowIndex = 0;
            const int recHeight = 130;
            const int recWidth = 150;

            var fontDisplayName = new Font("Khmer OS Metal Chrieng", 200, FontStyle.Regular);
            var fontBarCode = new Font("Free 3 of 9 Extended", 40, FontStyle.Regular);
            var solidBrush = new SolidBrush(Color.Black);

            while (_counter <= _productListt.Count - 1)
            {
                var posX = e.MarginBounds.Left - 50;
                if (rowIndex == 8)
                {
                    e.HasMorePages = true;
                    return;
                }

                var product = _productListt[_counter];
                Bitmap bitmapImage;
                if (string.IsNullOrEmpty(product.PhotoPath))
                    bitmapImage = Resources.NoImage;
                else
                {
                    var fileInfo = new FileInfo(product.PhotoPath);
                    bitmapImage = !fileInfo.Exists ? Resources.NoImage : new Bitmap(product.PhotoPath);
                }
                e.Graphics.DrawImage(
                    bitmapImage,
                    new Rectangle(posX, posY, 150, recHeight));

                posX += 20 + recWidth;
                var printStr = 
                    product.Description;
                var localPosY = (posY * 10) + (rowIndex * 250);
                TextRenderer.DrawText(
                    e.Graphics,
                    printStr,
                    fontDisplayName,
                    new Point(2600, Int32.Parse(localPosY.ToString())), 
                    SystemColors.WindowText,
                    TextFormatFlags.Default);

                printStr =                    
                    "Code: " + product.ProductCode;
                localPosY += 400;
                TextRenderer.DrawText(
                    e.Graphics,
                    printStr,
                    fontDisplayName,
                    new Point(2600, Int32.Parse(localPosY.ToString())),
                    SystemColors.WindowText,
                    TextFormatFlags.Default);

                printStr = "*" + product.ProductCode + "*";
                e.Graphics.DrawString(
                    printStr,
                    fontBarCode,
                    solidBrush,
                    posX,
                    posY + 90,
                    _strFormat);

                //printStr = product.QtyInStock.ToString("N0", AppContext.CultureInfo);
                printStr = string.Empty;
                posX =
                    e.MarginBounds.Right -
                    Int32.Parse(Math.Round(e.Graphics.MeasureString(printStr, fontDisplayName).Width, 0).ToString());
                e.Graphics.DrawString(
                    printStr,
                    fontDisplayName,
                    solidBrush,
                    posX,
                    posY + 15,
                    _strFormat);

                _counter++;
                posY += recHeight;
                rowIndex += 1;
            }
            e.HasMorePages = false;
        }

        private static void PrintDocBeginPrint(object sender, PrintEventArgs e)
        {
            try
            {
                _strFormat = new StringFormat
                                {
                                    Alignment = StringAlignment.Near,
                                    LineAlignment = StringAlignment.Center,
                                    Trimming = StringTrimming.EllipsisCharacter
                                };

                _counter = 0;
            }
            catch (Exception exception)
            {
                FrmExtendedMessageBox.UnknownErrorMessage(
                    Resources.MsgCaptionUnknownError,
                    exception.Message);
            }
        }
    }
}